﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq.Expressions;
using Newtonsoft.Json.Linq;

using CanDoo;
using CanDoo.Core;
using CanDoo.Data;
using CanDoo.Web;
using CanDoo.FineUI;
using CanDoo.Extensions;
using CanDoo.Oil.Core.Models;
using CanDoo.Oil.Core.Contracts;
using CanDoo.Admin.Core.Contracts;
using FineUIPro;

namespace CanDoo.Oil.WebApp.storeStock
{
    public partial class Edit : EditPagebase<IStockContract, Stock>
    {
        public IWarehouseContract WarehouseContract { get; set; }
        public IProductContract ProductContract { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //FineUIListHelper.BindDDL<Warehouse>(WarehouseContract.GetResolvedTree(), 0L, ed_Warehouse,true);
                //FineUIListHelper.BindDDL(WarehouseContract.EntityName, this.WarehouseContract.GetSelectList(), 0L, ed_Warehouse,true);
                FineUIBindList.BindEnable(ed_EnableAlerm,true);
                if (PageMode != pagemode.Add)
                    LoadData();
            }
        }

        protected override void LoadData(Stock entity)
        {
            lb_Product.SetValue(entity.Product.Name);
            //ed_Name.SetValue(entity.Name);
            if (!entity.WarehouseId.IsNull())
                lb_Warehouse.SetValue(entity.Warehouse.Name);
            ed_HighPrice.SetValue(entity.HighPrice);
            ed_LowPrice.SetValue(entity.LowPrice);
            ed_NowPrice.SetValue(entity.NowPrice);
            ed_CostPrice.SetValue(entity.CostPrice);
            ed_StockNum.SetValue(entity.StockNum);
            //ed_OnWayNum.SetValue(entity.OnWayNum);
            //ed_WaitBackNum.SetValue(entity.WaitBackNum);
            ed_ReasonableNum.SetValue(entity.ReasonableNum);
            ed_TopNum.SetValue(entity.TopNum);
            ed_BottomNum.SetValue(entity.BottomNum);
            ed_EnableAlerm.SetValue(entity.EnableAlerm);
            ed_Remark.SetValue(entity.Remark);
        }

        protected override OperationResult SaveData(Stock entity)
        {
            //entity.Name = ed_Name.GetValue().ToString();
            //if (ed_Warehouse.GetValue().ToLong() == -1)
            //    entity.WarehouseId = 0L;
            //else
            //    entity.WarehouseId = ed_Warehouse.GetValue().ToLong();
            //entity.HighPrice = ed_HighPrice.GetValue().ToDouble();
            //entity.LowPrice = ed_LowPrice.GetValue().ToDouble();
            //entity.NowPrice = ed_NowPrice.GetValue().ToDouble();
            //entity.CostPrice = ed_CostPrice.GetValue().ToDouble();
            //entity.StockNum = ed_StockNum.GetValue().ToDouble();
            //entity.OnWayNum = ed_OnWayNum.GetValue().ToDouble();
            //entity.WaitBackNum = ed_WaitBackNum.GetValue().ToDouble();
            entity.ReasonableNum = ed_ReasonableNum.GetValue().ToDouble();
            entity.TopNum = ed_TopNum.GetValue().ToDouble();
            entity.BottomNum = ed_BottomNum.GetValue().ToDouble();
            entity.EnableAlerm = ed_EnableAlerm.GetValue().ToBool();
            entity.Remark = ed_Remark.GetValue().ToString();
            return base.SaveData(entity);
        }
    }
}
